﻿using ASmile.Com.RoleUser;
using ASmile.DBModel;
using ASmile.Extend;
using System.Collections.Generic;
using System.Linq;

namespace ASmile.Com.Da
{
    public class RoleUserBLL : BLLBase, IRoleUserBLL
    {
        public ApiResultList<Sys_Role> GetRoleData(string searchVal)
        {
            return new RoleBLL().GetList(new ArgPageData {SearchVal = searchVal });

            //var query = DB.CreateQuery<Sys_Role>();
            //if (!serVal.IsEmpty())
            //{
            //    serVal = $"%{serVal}$";
            //    query.WhereAnd(s =>
            //        s.WEx_Like(s.RoleName, serVal) ||
            //        s.WEx_Like(s.RoleNamePY, serVal)
            //        );
            //}
            //var dt = DB.Query<Sys_Role>(query).ToList();
            //return RetOK(dt, dt.Count);
        }

        /// <summary>
        /// 获取指定角色的所有用户
        /// </summary>
        public ApiResultList<VSys_RoleUser> GetRoleUsers(ArgRoleUserSearch arg)
        {
            var query = DB.CreateQuery<VSys_RoleUser>();
            query.WhereAnd(s => s.RoleId == arg.RoleId);
            if (!arg.SearchVal.IsEmpty())
            {
                string likeStr = $"%{arg.SearchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.UserName, likeStr) ||
                    s.WEx_Like(s.LoginName, likeStr) ||
                    s.WEx_Like(s.MobileNumber, likeStr));
            }
            query.OrderBy(s => s.AddTime);
            var rList = DB.Query(query).ToList();
            return RetOK(rList, 0);
        }
        /// <summary>
        /// 获取指定角色下的所有用户Id列表
        /// </summary>
        public ApiResultList<string> GetRoleUserIds(ArgRoleUserSearch arg)
        {
            var emps = DB.Query<Sys_RoleUser>(s => s.RoleId == arg.RoleId).ToList(true);
            var empIds = emps.Select(s => s.UserId).ToList();
            return RetOK(empIds, empIds.Count);
        }

        public ApiResult AddUsers(ArgRoleUserAdd arg)
        {
            if (arg.RoleId.IsEmpty())
            {
                return RetErr("无效的角色");
            }

            if (arg.UserIds == null && arg.UserIds.Length == 0)
            {
                return RetErr("无效的用户");
            }

            var empList = DB.Query<Sys_RoleUser>(s => s.RoleId == arg.RoleId).ToList(true);

            List<string> addList = new List<string>();

            foreach (var item in arg.UserIds)
            {
                if (!empList.Exists(s => s.UserId == item))
                {
                    addList.Add(item);
                }
            }

            var m = new Sys_RoleUser();

            foreach (string empId in addList)
            {
                m.Id = NewId();
                m.RoleId = arg.RoleId;
                m.UserId = empId;
                DB.Insert(m);
            }
            return RetOK($"增加完成，数量：{addList.Count}");
        }

        public ApiResult Remove(ArgId arg)
        {
            var ret = DB.Delete<Sys_RoleUser>(s => s.Id == arg.Id);
            if (ret.RowCount == 1)
            {
                return RetOK("移除成功");
            }
            else
            {
                return RetErr("移除失败");
            }
        }
    }
}